import pandas as pd
from datetime import datetime

class DataProcessor:
    @staticmethod
    def get_cycle_time_info(step_data, cycle):
        """获取循环的时间信息"""
        try:
            prev_charge = step_data[(step_data["循环号"] == cycle) & 
                          (step_data["工步类型"] == "恒流充电")]
            current_discharge = step_data[(step_data["循环号"] == cycle + 1) & 
                                (step_data["工步类型"] == "恒流放电")]
            
            if prev_charge.empty or current_discharge.empty:
                return None
            
            time_format = "%Y-%m-%d %H:%M:%S"
            charge_end = datetime.strptime(str(prev_charge.iloc[0].iloc[6]), time_format)
            discharge_start = datetime.strptime(str(current_discharge.iloc[0].iloc[5]), time_format)
            #print(f"cycle: {cycle}, discharge_start: {discharge_start}, charge_end: {charge_end}")
            time_diff = round((discharge_start - charge_end).total_seconds() / 3600, 6)
            #print(f"time_diff: {time_diff}")
            return time_diff
        except Exception as e:
            print(f"获取循环时间信息出错: {e}")
            return None

    @staticmethod
    def get_cycle_group_index(cycle_group_indices, cycle):
        """获取循环所属的组索引"""
        index = 0
        start = 1
        while index >= 0:
            end = start + cycle_group_indices[index] - 1
            if cycle >= start and cycle <= end:
                return index
            index += 1
            start = end + 1
        return -1
    
    @staticmethod
    def get_discharge_group_cap(cycle_data):
        "获取整十循环的放电量"
        discharge_group_cap = [0]
        #print(f"{cycle_data.shape[0] // 10}?????")
        for index in range(cycle_data.shape[0]):
            cycle = index + 1
            if cycle % 10 == 0 and cycle >=10:
                discharge_group_cap.append(cycle_data.get("放电容量(Ah)")[index])
        return discharge_group_cap
    
    @staticmethod
    def get_discharge_voltage_group_cap(recorde_data, position):
        "获取每个循环下的每个三十秒的放电容量和电压"
        discharge_voltage_group = []
        
        for index in range(position, recorde_data.shape[0]):
            recorde_data_row = recorde_data.iloc[index]
            #print(index, recorde_data_row)
            if recorde_data_row["工步类型"] == "恒流放电":
                discharge_voltage_group.append(recorde_data_row["电压(V)"])
                discharge_voltage_group.append(recorde_data_row["容量(Ah)"])
               # print(f"discharge_voltage_group: {discharge_voltage_group}***************")
            elif recorde_data_row["工步类型"] == "搁置":
                position = index
                break

        return discharge_voltage_group, position












